package com.mobilesales.android.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import com.mobilesales.android.R;

public class DateUtil {

	private static final String format = "MM/dd/yyyy HH:mm:ss a";
	
	public static Date parseDate(String dateString) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.US);
			Date d = sdf.parse(dateString);
			return d;
		}
		catch(Exception ex) {
			return null;
		}
	}
	
	public static Date parseShortDate(String dateString) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
			Date d = sdf.parse(dateString);
			return d;
		}
		catch(Exception ex) {
			return null;
		}
	}
	
	public static Date parseTime(String dateString) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("HH:mm", Locale.ENGLISH);
			Date d = sdf.parse(dateString);
			return d;
		}
		catch(Exception ex) {
			return null;
		}
	}		
	
	public static String toDateString(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.ENGLISH);
		return sdf.format(date);
	}
	
	public static String toShortDateString(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
		return sdf.format(date);
	}
	
	public static String toTimeString(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm", Locale.ENGLISH);
		return sdf.format(date);
	}

	public static String toMessageDateString(String sendDate) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.ENGLISH);
			Date d = sdf.parse(sendDate);
			Calendar current = Calendar.getInstance();
			Calendar sent = Calendar.getInstance();
			sent.setTime(d);
			
			int year = sent.get(Calendar.YEAR);
			int day = sent.get(Calendar.DATE);
			int month = sent.get(Calendar.MONTH);
			int hour = sent.get(Calendar.HOUR_OF_DAY);
			int minute = sent.get(Calendar.MINUTE);
			
			
			if (current.get(Calendar.YEAR) == year) {
				if (current.get(Calendar.DATE) == day &&
					current.get(Calendar.MONTH) == month) {
					return addLeadingZero(hour)+":"+addLeadingZero(minute);
				}
				else {
					return addLeadingZero(day)+"."+addLeadingZero(month+1)+" "+addLeadingZero(hour)+":"+addLeadingZero(minute);
				}
			}
			else {
				return addLeadingZero(day)+"."+addLeadingZero(month+1)+"."+year;
			}
		}
		catch(Exception ex) {
			return sendDate;
		}
	}
	
	public static String toTimeDistance(String date1, String date2) {
    	String startTime = date1.split(" ")[1];
    	String endTime = date2.split(" ")[1];
    	if (startTime.length() > 5)
    		startTime = startTime.substring(0, 5);
    	if (endTime.length() > 5)
    		endTime = endTime.substring(0, 5);
    	
    	return String.format("%s - %s", startTime,endTime);
	}
	
	private static String addLeadingZero(int number) {
		if (number>=0 && number < 10) {
			return "0"+number;
		}
		return String.valueOf(number);
	}
	
	public static int[] monthNamesStringResourceIds() {
		return new int[] { R.string.January,
				R.string.February, R.string.March, R.string.April,
				R.string.May, R.string.June, R.string.July, R.string.August,
				R.string.September, R.string.October, R.string.November,
				R.string.December };
	}
	
}



